/**
 * ACCESS TO THE OBJECT, AND MANIPULATE THE CAMERA.
 */
package  
{
	import flare.basic.*;
	import flare.core.*;
	import flare.loaders.collada.*;
	import flare.system.*;
	import flare.utils.*;
	import flash.display.*;
	import flash.events.*;
	
	[SWF(frameRate = 120, backgroundColor = 0x808080, width = 800, height = 600 )]
	/**
	 * ...
	 * @author Ariel Nehamd
	 */
	public class Test03 extends Sprite
	{
		private var scene:Scene3D;
		private var model:Pivot3D;
		
		public function Test03() 
		{
			ColladaLoader.texturesFolder = "textures";
			
			scene = new Scene3D( this );
			scene.camera.setPosition( 0, 0, -170 );
			//scene.antialias = 1;
			
			model = scene.addChildFromFile( "car.dae" );
			
			scene.addEventListener( Scene3D.PROGRESS_EVENT, progressEvent );
			scene.addEventListener( Scene3D.COMPLETE_EVENT, completeEvent );
			
			stage.scaleMode = "noScale";
			stage.align = "tl";
			stage.addEventListener( Event.RESIZE, resirEvent );
		}
		
		private function resirEvent(e:Event):void 
		{
			scene.setViewport( 0, 0, stage.stageWidth, stage.stageHeight );
		}
		
		private function progressEvent(e:Event):void 
		{
			trace( scene.loadProgress );
		}
		
		private function completeEvent(e:Event):void 
		{
			// once we loaded the scene, we start to update.
			scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent );
		}
		
		private function updateEvent(e:Event):void 
		{
			// is better to store the 3d objects in variables, but to keep the
			// code simple....
			scene.getChildByName( "AXTruck-FLWheel001" ).rotateX( 10 );
			scene.getChildByName( "AXTruck-BLWheel001" ).rotateX( 10 );
			scene.getChildByName( "AXTruck-FLWheel" ).rotateX( 10 );
			scene.getChildByName( "AXTruck-BLWheel" ).rotateX( 10 );			
			
			// simple orbit camera.
			if ( Input3D.mouseDown )
			{
				scene.camera.rotateY( Input3D.mouseXSpeed, false, Vector3DUtils.ZERO );
				scene.camera.rotateX( Input3D.mouseYSpeed, true, Vector3DUtils.ZERO );
			}
			
			scene.camera.translateZ( scene.camera.getPosition().length * Input3D.delta / 20 );
		}
		
	}

}